<?php
/**
 *+------------------
 * Lflow
 *+------------------
 * Copyright (c) 2023~2030 gitee.com/liu_guan_qing All rights reserved.本版权不可删除，侵权必究
 *+------------------
 * Author: Mr.April(405784684@qq.com)
 *+------------------
 */
declare (strict_types=1);

namespace lflow\entity;

use lflow\helper\JsonHelper;
use lflow\workflow\WorkFlowEngine;
use think\Exception;

class HistoryOrder
{
    /**
     * 主键ID
     */
    private string $id;
    /**
     * 流程定义ID
     */
    private string $process_id;

    /**
     * 流程名称
     */
    private string $process_name;

    /**
     * 流程实例状态（0：结束；1：活动）
     */
    private int $order_state;
    /**
     * 流程实例创建者ID
     */
    private string $creator;
    /**
     * 流程实例创建时间
     */
    private string $create_time;
    /**
     * 流程实例结束时间
     */
    private string $end_time;
    /**
     * 流程实例为子流程时，该字段标识父流程实例ID
     */
    private string $parent_id;
    /**
     * 流程实例期望完成时间
     */
    private string $expire_time;
    /**
     * 流程实例优先级
     */
    private ?int $priority;
    /**
     * 流程实例编号
     */
    private string $order_no;
    /**
     * 流程实例附属变量
     */
    private string $variable;

    /**
     * 根据Order实例构建历史记录实例
     *
     * @param \lflow\entity\Order $order
     */
    public function historyOrder(Order $order): void
    {
        $this->id          = $order->getId();
        $this->process_id  = $order->getProcessId();
        $this->create_time = $order->getCreateTime();
        $this->expire_time = $order->getExpireTime();
        $this->creator     = $order->getCreator();
        $this->parent_id   = $order->getParentId();
        $this->priority    = $order->getPriority();
        $this->order_no    = $order->getOrderNo();
        $this->variable    = $order->getVariable();
    }

    /**
     * 根据历史实例撤回活动实例
     *
     * @return \lflow\entity\Order  活动实例对象
     */
    public function undo(): Order
    {
        $order = new Order();
        $order->setId($this->id);
        $order->setProcessId($this->process_id);
        $order->setParentId($this->parent_id);
        $order->setCreator($this->creator);
        $order->setCreateTime($this->create_time);
        $order->setLastUpdator($this->creator);
        $order->setLastUpdateTime($this->end_time);
        $order->setExpireTime($this->expire_time);
        $order->setOrderNo($this->order_no);
        $order->setPriority($this->priority);
        $order->setVariable($this->variable);
        $order->setVersion(0);
        return $order;
    }

    public function getProcessId(): string
    {
        return $this->process_id;
    }

    public function setProcessId(string $processId): void
    {
        $this->process_id = $processId;
    }

    public function getOrderState(): int
    {
        return $this->order_state ?? 0;
    }

    public function setOrderState(int $orderState): void
    {
        $this->order_state = $orderState;
    }

    public function getCreator(): string
    {
        return $this->creator ?? '';
    }

    public function setCreator(string $creator)
    {
        $this->creator = $creator;
    }

    public function getCreateTime()
    {
        return $this->create_time ?? '';
    }

    public function setCreateTime(string $createTime): void
    {
        $this->create_time = $createTime;
    }

    public function getEndTime(): string
    {
        return $this->end_time ?? '';
    }

    public function setEndTime(string $endTime): void
    {
        $this->end_time = $endTime;
    }

    public function getParentId(): string
    {
        return $this->parent_id ?? '';
    }

    public function setParentId(string $parentId)
    {
        $this->parent_id = $parentId;
    }

    public function getExpireTime(): string
    {
        return $this->expire_time ?? '';
    }

    public function setExpireTime(string $expireTime): void
    {
        $this->expire_time = $expireTime;
    }

    public function getPriority(): int
    {
        return $this->priority ?? 1;
    }

    public function setPriority(int $priority): void
    {
        $this->priority = $priority;
    }

    public function getId(): string
    {
        return $this->id ?? '';
    }

    public function setId(string $id): void
    {
        $this->id = $id;
    }

    public function getOrderNo(): string
    {
        return $this->order_no ?? '';
    }

    public function setOrderNo(string $orderNo): void
    {
        $this->order_no = $orderNo;
    }

    public function getVariable(): string
    {
        return $this->variable ?? '';
    }

    public function setVariable(string $variable): void
    {
        $this->variable = $variable;
    }

    public function getVariableMap(): object
    {
        $map = JsonHelper:: fromJson($this->getVariable(), false);
        if ($map == null) return new \stdClass();
        return $map;
    }

    public function getProcessName(): string
    {
        $engine  = new WorkFlowEngine();
        $process = $engine->process()->getProcessById($this->process_id);
        if (empty($process)) {
            return $this->process_id ?? '';
        }
        return $process->getDisplayName();
    }

    public function toString(): string
    {
        return json_encode(get_object_vars($this));

    }

    public function toArray(): array
    {
        return get_object_vars($this);
    }
}